﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace MD5加密
{
	class Program
	{
		private static string md5;

		static void Main(string[] args)
		{
			//202cb962ac59075b964b07152d234b70
			string s = GetMD5("123");

			Console.WriteLine(s);
			Console.ReadLine();
		}
		public static string GetMD5(string str)
		{
			//将字符串转换为字节数组
			byte[] buffer= Encoding.UTF8.GetBytes(str);
			//创建MD5对象
			MD5 md5 = MD5.Create();
			//返回一个加密好的字节数组
			byte[] MD5Buffer = md5.ComputeHash(buffer);
			//将在字节数组转换字符串
			//将字节数组中每个元素按照指定的编码格式解析成字符串 //不可行
			//直接将数组Tostring();
			//将字节数组中的每个元素Tostring();				//可行
			//return Encoding.UTF8.GetString(MD5Buffer);
			string newstr = "";
			for (int i = 0; i < MD5Buffer.Length; i++)
			{
				newstr += MD5Buffer[i].ToString("x2");
			}
			return newstr;
		}
	}
}
